$(function(){
	var basePath=$("#basepath").val();
	$("#studentFilter_form select[name='college']").change(function(){
		var collegeId=$(this).val();
		var $depSelect=$("#studentFilter_form select[name='department']");
		$depSelect.html("");
		var $classSelect=$("#studentFilter_form select[name='classInfo']");
		$classSelect.html("");
		var $goBtn=$("#studentFilter_go");
		$.get(basePath+ "admin/branch/getDepsOfCollege/"+collegeId,{},
				function(data,textStatus){
					if(textStatus=="success"){
						if(data.error>0){
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							return;
						}
						for(i=0;i<data.length;i++){
							$(" <option/>").val(data[i].id).html(data[i].name).appendTo($depSelect);
						}
						if(data.length==0){
							$(" <option value='0'>无数据</option>").appendTo($depSelect);
							$(" <option value='0'>无数据</option>").appendTo($classSelect);
							$goBtn.attr("disabled",true); 
						}else{
							$goBtn.attr("disabled",false); 
							$depSelect.change();
						}
					}
			}, "json");
	});
	$("#studentFilter_form select[name='department']").change(function(){
		var departmentId=$(this).val();
		var $classSelect=$("#studentFilter_form select[name='classInfo']");
		$classSelect.html("");
		var $goBtn=$("#studentFilter_go");
		$.get(basePath+ "admin/class/getClassesByDep/"+departmentId,{},
				function(data,textStatus){
					if(textStatus=="success"){
						if(data.error>0){
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							return;
						}
						for(i=0;i<data.length;i++){
							$(" <option/>").val(data[i].id).html(data[i].name).appendTo($classSelect);
						}
						if(data.length==0){
							$(" <option value='0'>无数据</option>").appendTo($classSelect);
							$goBtn.attr("disabled",true); 
						}else{
							$goBtn.attr("disabled",false); 
						}
					}
			}, "json");
	});
	$("#studentFilter_go").click(function(){
		var classId=$("#studentFilter_form select[name='classInfo']").val();
		var depId=$("#studentFilter_form select[name='department']").val();
		var clgId=$("#studentFilter_form select[name='college']").val();
		if(classId==0){
			return;
		}
		window.location=basePath+"admin/student/showStudentList/"+clgId+"/"+depId+"/"+classId+"/1";
	});
	$(".student_edit").click(function(){
		var stuId=$(this).attr("data-studentId");
		window.location=basePath+"admin/student/showUpdateStudent/"+stuId;
	});
	$("#student_add").click(function(){
		window.location=basePath+"admin/student/showInsertStudent";
	});
	$(".student_delete").click(function(){
		$("#student_delete_box input[name='stuId']").val($(this).attr("data-studentId"));
	});
	$("#student_delete_box_delete").click(function(){
		var stuId=$("#student_delete_box input[name='stuId']").val();
		$.get(basePath+ "admin/student/deleteStudent/"+stuId,{},
				function(data,textStatus){
					if(textStatus=="success"){
						switch (data) {
						case "-1":
							alert("请勿进行非法操作");
							window.location=basePath;
							break;
						case "-2":
							noty({text:"删除错误:此学生已经选择了意向题目,请清除之后再试",type:"error"});
							$("#student_delete_box").modal('hide');
							break;
						case "-3":
							noty({text:"删除错误:此学生已经选择了毕业设计,请清除之后再试,",type:"error"});
							$("#student_delete_box").modal('hide');
							break;
						case "0":
							noty({text:"删除成功,正在刷新"});
							$("#student_delete_box").modal('hide');
							history.go(0);
							break;
						default:
							alert("登陆状态已过期,请重新登陆");
							window.location=basePath;
							break;
						}
					}
			}, "text");
	});
	$("#student_add_excel").click(function(){
		$("#student_add_excel_box_confirm").unbind();
		$("#student_add_excel_box_file_name").html("未选择");
		$("#student_add_excel_box_alert_container").html("");
		$("#student_add_excel_box_file").show();
		$("#student_add_excel_box_progress").hide();
		$("#student_add_excel_box_progress div").css("width","0%");
	});
	$("#student_add_excel_box_file input[type='file']").fileupload({  
	    url:basePath+"admin/student/insertStudentByExcel",//文件上传地址，当然也可以直接写在input的data-url属性内  
	    autoUpload:false,
	    done:function(e,result){  
			noty({text:"导入完成,正在处理"});
	    	window.location=basePath+"/admin/student/showInsertReport";
	    } ,
	    progress:function(e,result){
	    	var progress=parseInt(result.loaded / result.total * 100, 10);
	    	$("#student_add_excel_box_progress div").css("width",progress+"%");
	    },
	    add:function(e,result){
	    	var fileName=result.files[0].name.toLowerCase();
			var isExcel=/(\.xlsx{0,1})$/.test(fileName);
			if(isExcel){
				$("#student_add_excel_box_alert_container .wrong_file_error").alert('close');
				$("#student_add_excel_box_file_name").html(fileName);
			}else{
				makeAlert("请选择正确的excel文件","error",$("#student_add_excel_box_alert_container"),"wrong_file_error");
				$("#student_add_excel_box_file_name").html("请选择");
				return;
			}
			$("#student_add_excel_box_confirm").unbind();
			$("#student_add_excel_box_confirm").click(function(){
				$("#student_add_excel_box_file_name").html("正在上传:"+fileName);
				$("#student_add_excel_box_file").hide();
				$("#student_add_excel_box_progress").show();
				result.submit();
			});
	    }
	}) ;
});